<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
	/**
	 * 发布文章
	 * @param $data
	 */
	function add($data = array()){
		$this->mycache->delete('site_group_list_'.$data['fgroup']);
		$this->db->insert('rsh_site', $data);
		return $this->db->insert_id();
	}
	

	function update($id, $data = array()){
		$this->mycache->delete('site_group_list_'.$data['fgroup']);
		$this->mycache->delete('site_group_obj_'.$id);
		$this->db->where('id', $id);
		$this->db->update('rsh_site', $data);
		return $this->db->affected_rows();
	}
	
	function get_site_obj($id){
		$mkey = "site_group_obj_".$id;
		$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		
		$select = "id,title,url,fgroup,forder,content,parent_id,type_set";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_site');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
	function get_fgroup(){
		$sql = "select distinct fgroup from rsh_site where fstatus=1";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
    
	function get_site_list($data){
		//封装查询内容
		$select = "id, title, url, fgroup, forder, content";
		
		//封装条件
		$_where = " where is_del=0 ";
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['fgroup']) && $data['where']['fgroup'] != ''){
			$_where .= " and fgroup like '%".$data['where']['fgroup']."%'";
		}
		if(isset($data['where']['url']) && $data['where']['url'] != ''){
			$_where .= " and url='".$data['where']['url']."'";
		}
		
		//排序
		$order  = " order by fgroup,forder desc,id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_site";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['site_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_site";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['site_count'] = $query->row()->count;
		return $data;
	}
	

	function get_site_group_list($data){
		$mkey = "site_group_list_".$data['where']['fgroup'];
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$select = "id, title, url, fgroup, forder, content";
		
		//封装条件
		$_where = " where is_del=0 ";
		if(isset($data['where']['fgroup']) && $data['where']['fgroup'] != ''){
			$_where .= " and fgroup='".$data['where']['fgroup']."'";
		}
		
		//封装sql
		$sql_result = "select $select from rsh_site";
		$sql_result .= $_where;
		$sql_result .= "order by forder asc";
		$query = $this->db->query($sql_result);
		$list = $query->result_array();
		$this->mycache->set_array($mkey,$list);
		return $list;
	}
	
	function get_site_title_array(){
		
		//封装sql
		$sql_result = "select id, title from rsh_site where type_set=1 and is_del = 0 ";
		$sql_result .= "order by forder asc";
		
		$query = $this->db->query($sql_result);
		
		$list = array();
		
		foreach($query->result_array() as $val){
			$list[$val['id']] = $val['title'];			
		}
		
		return $list;
	}
	
	function get_site_category_list($data){
		
		if(isset($data['where']['parent_id']) && $data['where']['parent_id'] != ''){
			$_where = " where is_del=0  and ( parent_id=".$data['where']['parent_id'].' or id='.$data['where']['parent_id'].')';
		}
		else $_where = " where is_del=0 and parent_id=0";
		
		//排序
		$order  = "  order by id desc,forder asc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select * from rsh_site";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$list = $query->result_array();
		$data_list = array();
		if($list){
			foreach($list as $val){
				$category_list = $this->get_site_category_by_pid($val['id']);
				$data_list[$val['id']] = $val;
				if($category_list){
					foreach($category_list as $v){
						
						
						$second_list = $this->get_site_category_by_pid($v['id']);
						if($second_list){
							foreach ($second_list as $s){
								$v['second_list'][] = $s;
							}
						}
						
						$data_list[$val['id']]['category_list'][] = $v;
						
					}
				}
			}
		}
		$data['site_list']= $data_list;
		//总记录条数
		$sql_count = "select count(*) as count from rsh_site";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['site_count'] = $query->row()->count;
		
		return $data;
	}
	
	
	function get_site_category_by_pid($parent_id,$select='*'){
		$sql_result = "select ".$select." from rsh_site where is_del=0 and parent_id=".$parent_id." order by forder asc";
		$query = $this->db->query($sql_result);
		$list = $query->result_array();
		return $list;
	}
	
	function get_site_select_array(){
		$_where = " where is_del=0 and parent_id=0";
		$order  = " order by forder asc ";
		$sql_result = "select id,title from rsh_site";
		$sql_result .= $_where;
		$sql_result .= $order;
		$query = $this->db->query($sql_result);
		$list = $query->result_array();
		$data_list = array();
		if($list){
			foreach($list as $val){
				$category_list = $this->get_site_category_by_pid($val['id']);
				$data_list[$val['id']] = $val;
				if($category_list){
					foreach($category_list as $v){
						$data_list[$val['id']]['category_list'][] = $v;
					}
				}
			}
		}
		return $data_list;
	}
	
	function get_site_select_parent_array(){
		$_where = " where is_del=0 and parent_id=0";
		$order  = " order by forder asc,id desc ";
		$sql_result = "select id,title from rsh_site";
		$sql_result .= $_where;
		$sql_result .= $order;
		$query = $this->db->query($sql_result);
		$list = $query->result_array();
		$data_list = array();
		if($list){
			foreach($list as $val){
				$data_list[$val['id']] = $val;
			}
		}
		return $data_list;
	}
	
	function get_site_one($where = array(),$select='*'){
		$this->db->select($select);
		$this->db->where($where);
		$this->db->limit(1);
		
		$query = $this->db->get('rsh_site');
		
		return $query->row_array();
	}
	
	function get_site_url_list($url){
		$select = "*";
		$this->db->select($select);
		$this->db->where(array('url'=>$url,'parent_id'=>0,'is_del'=>0));
		$query = $this->db->get('rsh_site');
		$retsult = $query->row_array();
		
		if($retsult){
			$category_list = $this->get_site_category_by_pid($retsult['id']);
			//$data_list[] = $retsult;
			if($category_list){
				
				foreach($category_list as $v){
					$data_list[] = $v;
				}
				
			}
		}
		
		return $data_list;
	}
	
	function get_site_url_news(){
		$select = "id,title,url,fgroup,forder,content,parent_id,type_set";
		$this->db->select($select);
		$this->db->where(array('type_set'=>1,'parent_id'=>0,'is_del'=>0));
		$query = $this->db->get('rsh_site');
		$data_list = $query->result_array();
		return $data_list;
	}
	
	function get_site_search_array(){
		
		//封装sql
		$sql_result = "select id, title from rsh_site where type_set=1 and is_del = 0 ";
		$sql_result .= "order by forder asc";
		
		$query = $this->db->query($sql_result);
		
		$list = array();
		
		foreach($query->result_array() as $val){
			$list[$val['id']] = $val['title'];
		}
		
		return $list;
	}
	
	
}